
package sc.tools;

import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.util.Base64;


/** TransCodeTool.java: 
 * 数据类型转化类
 * 
 * ----- 2020-05-25 下午00:21:30 scimence */
public class TransCodeTool
{
	
	
	/** 每两个字符还原为一个字节 */
	private static byte[] toBytes(String Hex)
	{
		byte[] B = new byte[Hex.length() / 2];
		for (int i = 0; i + 1 < Hex.length(); i += 2)
		{
			String hexStr = Hex.substring(i, i + 2);
			B[(i / 2)] = toByte(hexStr);
		}
		
		return B;
	}
	
	private static byte toByte(String Hex)
	{
		int N = ToInt(Hex.charAt(0)) * 16 + ToInt(Hex.charAt(1));
		return (byte) N;
	}
	
	private static int ToInt(char c)
	{
		if ('0' <= c && c <= '9')
			return (int) (c - '0');
		else if ('a' <= c && c <= 'z')
			return (int) (c - 'a' + 10);
		else return -1;
	}
	
	// ------------------
	// "thumbsup_close",
	// "iVBORw0KGgoAAAANSUhEUgAAACAAAAAfCAYAAACGVs+MAAAABHNCSVQICAgIfAhkiAAAAo5JREFUSInFl79rE2EYxz9vQkKi3lXqUGjBlDYGOggtFcRJCDgUhBQphEADKZSCk+Lmdv+ARXQTnDp0cWqlIEihP4IZFKUdCqVSghpMwdD0SoOXK+dgUy7JJb17U+sz3T3vEz4fjvf95k5wUhoEu1V1BsuaAO4AQc63DOADQrwpHRy80v7eIwBmw+G+QCDw1rKs4XOGOpYQ4ku1Wr3/pFL5ITQIdivKR+DmRcBttVnS9Vv+B6r6EMhcMBygJxwK7fnHgsFnwPVatzce58bUFJf7+ihtbHRMiU5O0j8xgRACfXe3cVkVLxTlNycbTo1GubewgPD7Afg6P89nTZOGj2gag6kUANbxMe/Hxylvb9tHDB+23d4Vi53CAQZTKUYkBexwAOH3o0ajjWNBn/2umM1yVCjUTchINMIBjgoFitls02ydgFEus5rJUCkWpSWc4JVikdVMBqNcbi8AcJjPs5JOS0m0gq+k0xzm846/aRKQlZCBtxTwKiELbyvgVqITOIB4oSjWWUNXIhHuzs0R7ump6x/s7DQdLS9wOOMJ1KrVk+gU7lqgnUQncE8CNYm9XM5xbS+X8wz3LDCiaUQSCce1SCIhFduuBZx2e2PJxLYrgVZHbX16uqPYdiXQ7pz/XFuTjm1XAm5CppP/jrYCXhKuEwlHAZl4lZVoEhhIJqWzvZ3EQDLZUsCwN7qH6z8NvCZcK4lro6OO4z7gk73zfWnp9PqoUJCK15qE/fXu2+Ki0+imeKmqjyzLem7vdsViXB0aorC8TFXXPcHtFVAUeuNx9re2Gt+GARBCPP7vX0Y+DQzTNMeAzYuEm6Y5poHhB3hnmvptw3h9KRT6BYSB/n8EXhdCzJZ0feapae4D/AHqo5v2YwYmpQAAAABJRU5ErkJggg==");
	
	/** 图片转string */
	public static String ToString(Bitmap bitmap)
	{
		ByteArrayOutputStream outStream = new ByteArrayOutputStream(); // outputstream
		bitmap.compress(CompressFormat.PNG, 100, outStream); // 保存图像到输出流中
		byte[] bytes = outStream.toByteArray(); // 转为byte数组
		return Base64.encodeToString(bytes, Base64.DEFAULT); // Base64编码数组
	}
	
	/** string数据转bitmap */
	public static Bitmap ToBitmap(String bitmapData)
	{
		Bitmap bitmap = null;
		try
		{
			// out = new FileOutputStream("/sdcard/aa.jpg");
			byte[] bitmapArray = Base64.decode(bitmapData, Base64.DEFAULT);
			bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
			// bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
			return bitmap;
		}
		catch (Exception e)
		{
			return null;
		}
	}
	
	/** 数组转化为string */
	public static String ToString(byte[] bytes)
	{
		return Base64.encodeToString(bytes, Base64.DEFAULT); // Base64编码数组
	}
	
	/** string还原为bytes数组 */
	public static byte[] ToBytes(String bytesStr)
	{
		byte[] bitmapArray = Base64.decode(bytesStr, Base64.DEFAULT);
		return bitmapArray;
	}
	
}
